package com.classic.okhttp.defer.impl;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Rockey
 */
public class RkyLog implements ProtocolLogger {
    public static boolean m_bPrintLog = false;//开启日志
    private static RkyLog m_ins = null;
    private Map<String, Boolean> mSqlError = new HashMap<String, Boolean>();

    public static RkyLog getIns() {
        if (m_ins == null) {
            synchronized (RkyLog.class) {
                if (m_ins == null) {
                    m_ins = new RkyLog();
                }
            }
        }

        return m_ins;
    }

    public boolean needPrint() {
        return m_bPrintLog;
    }

    /**
     * 纪录到本地文件
     * @param msg
     */
    public void eLog(String msg)
    {
        info("[error :"+msg);
    }
    /**
     * 纪录到本地文件
     * @param msg
     */
    public void info(String msg) {
//        if (msg != null && needPrint()) {
//            v("", msg);
//            String errorTag = "[error :";
//            String socketTag = "[socket]";
//            String socketConenctTag = "[connect socket]";
//            String httpTag = "[http";
//            String dazeTag = "<GDAZE";
//            String IOController = "[IOController";
//
//            int index = msg.indexOf(IOController);
//            if (index != -1) {
//                CacheManager.get().writeCustomLog(msg, "IOController");
//                return;
//            }
//
//            index = msg.indexOf(socketTag);
//
//            if (index != -1) {
//                CacheManager.get().writeCustomLog(msg, "behindSocket");
//
//                return;
//            }
//
//            index = msg.indexOf(socketConenctTag);
//
//            if (index != -1) {
//                CacheManager.get().writeCustomLog(msg, "behindSocket");
//                return;
//            }
//
//            index = msg.indexOf(errorTag);
//
//            if (index != -1) {
//                CacheManager.get().writeCustomLog(msg, "errorLog");
//                return;
//            }
//
//            index = msg.indexOf(httpTag);
//
//            if (index != -1) {
//                CacheManager.get().writeCustomLog(msg, "behindHttp");
//
//                /**
//                 * @caution
//                 *
//                 *          should add return as if http request contains
//                 *          keywords sqlError will cause below storeError Report
//                 *          and dead circle
//                 *
//                 * @author rockey
//                 *
//                 */
//                return;
//            }
//
//
//            CacheManager.get().writeCustomLog(msg, "log");
//            String sqlError = "^execSql Error(.)*";
//            Pattern pattern = Pattern.compile(sqlError);
//            Matcher matcher = pattern.matcher(msg);
//
//            /**
//             * @caution
//             *
//             *          1. store sql may cause sqlException then dead cirle. Add
//             *          5 seconds mechanism to solve this problem. 2. upload sql
//             *          error http log may cause store error log again. Add http
//             *          log return to solve this problem.
//             *
//             * @author rockey
//             */
//
//            if (matcher.matches()) {
//                if (mSqlError.get(msg) == null) {
//                    if (mSqlError.size() > 100) {
//                        Map<String, Boolean> temp = new HashMap<String, Boolean>();
//                        mSqlError = temp;
//                    }
//
//                    mSqlError.put(String.valueOf(msg), true);
//                    //        UploaderManager.get().storeJavaBehindErrorLog(msg);
//                }
//            }
//        }
    }

    @Override
    public void receive(String s) {
        if (s != null) {
            v("", s);
        }
    }

    @Override
    public void send(String s) {
        if (s != null) {
            v("", s);
        }
    }

    static public void v(String tag, String format, Object... args) {
        if (m_bPrintLog) {
            if (tag == null) {
                tag = "";
            }
            String prefix = "";
            String string = format;
            try {
                string = prefix + String.format(format, args);
            } catch (Exception e) {
            }

            if (string == null) {
                string = "";
            }
            Log.v(tag, string);
        }
    }

    static public void i(String tag, String format, Object... args) {
        if (m_bPrintLog) {
            if (tag == null) {
                tag = "";
            }
            String prefix = "";
            String string = format;
            try {
                string = prefix + String.format(format, args);
            } catch (Exception e) {
            }

            if (string == null) {
                string = "";
            }
            Log.i(tag, string);
        }
    }

    static public void e(String tag, String format, Object... args) {
        if (m_bPrintLog) {
            if (tag == null) {
                tag = "";
            }
            String prefix = "";
            String string = format;
            try {
                string = prefix + String.format(format, args);
            } catch (Exception e) {
            }

            if (string == null) {
                string = "";
            }
            Log.e(tag, string);
        }
    }

    static public void d(String tag, String format, Object... args) {
        if (m_bPrintLog) {
            if (tag == null) {
                tag = "";
            }
            String prefix = "";
            String string = format;
            try {
                string = prefix + String.format(format, args);
            } catch (Exception e) {
            }

            if (string == null) {
                string = "";
            }
            Log.d(tag, string);

        }
    }

    static public void w(String tag, String format, Object... args) {
        if (m_bPrintLog) {
            if (tag == null) {
                tag = "";
            }
            String prefix = "";
            String string = format;
            try {
                string = prefix + String.format(format, args);
            } catch (Exception e) {
            }

            if (string == null) {
                string = "";
            }
            Log.w(tag, string);

        }
    }

    @Override
    public void logThrowable(Throwable throwable) {
        if (throwable == null) {
            return;
        } else {
            info("=====");
        }
//        UploaderManager.get().storeJavaBehindErrorLog(
//                "logThrowable:\nMessage:" + String.valueOf(throwable.getMessage()) + "\n\nException:"
//                        + AvqUtils.string.getThrowableDescription(throwable));
    }

}
